/**
 * Created by Administrator on 2016/5/7.
 */
const fs = require('fs');
const http = require('http');

http.createServer(function (request, response) {
    var uploadedLength = 0;
    var fileLength = request.headers['content-length'];
    var chunk = 0;
    var file = fs.createWriteStream('F:/a.pdf');
    // 打印上传进程
    request.on('readable', function () {
        while (null !== (chunk = request.read())) {
            uploadedLength += chunk.length;
            var prog = uploadedLength/fileLength*100;
            response.write("Progress: " +  parseInt(prog,10) + "%\n");
        }
    });

    // 上传结束后，打印消息
    request.on('end', function () {
        response.end('Upload finished!');
    })

    request.pipe(file, {end: false});
}).listen(8080);
